package com.hdax.controller;

import com.hdax.entity.Department;
import com.hdax.entity.EmployeeInfo;
import com.hdax.service.DepartmentService;
import com.hdax.service.EmployeeInfoService;
import com.hdax.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 部门控制器
 *
 * @author Aizen
 * @date 2022/05/09
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {

    /**
     * 部门服务
     */
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private EmployeeInfoService employeeInfoService;

    /**
     * 返回 部门页面
     * @return 页面
     */
    @GetMapping("/index")
    public String Department(){
        return "system/Department";
    }

    /**
     * 部门列表
     *
     * @return 响应体
     */
    @ResponseBody
    @GetMapping("/list")
    public ResultUtil index() {
        List<Department> list = departmentService.findAll();
        return ResultUtil.splitPage(list,list.size());
    }

    /**
     * 添加部门页面
     *
     * @param map 集合
     * @return 页面
     */
    @GetMapping("/addDepartment")
    public String addDepartment(Map map){
        map.put("emps",employeeInfoService.getAllEmployeeInfo());
        return "system/addDepartment";
    }

    /**
     * 添加部门
     *
     * @param department 部门
     * @return {@link ResultUtil}
     */
    @ResponseBody
    @PostMapping("/addDepartment")
    public ResultUtil addDepartment(Department department){
        return departmentService.addDepartment(department);
    }
    @GetMapping("/openEditDep/{id}")
    public String openEditDep(@PathVariable("id") Integer id,Map map){
        //获取到部门的信息
        Department department = departmentService.findById(id);
        //获取该部门经理的信息
        EmployeeInfo employeeInfo = employeeInfoService.getEmployeeInfoById(department.getDperson());

        map.put("employeeInfo",employeeInfo);
        map.put("department",department);
        map.put("emps",employeeInfoService.getAllEmployeeInfo());

        return "system/editDepartment";
    }
    @ResponseBody
    @PostMapping("/edit")
    public ResultUtil edit(Department department){
        return departmentService.updateDepartment(department);
    }
    @ResponseBody
    @PostMapping("/delete")
    public ResultUtil del(Integer depId){
        return departmentService.deleteDepartment(depId);
    }




}
